package com.qf.interceptor;

import com.qf.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zdl
 * @date 2025/2/8
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
        User username = (User)request.getSession().getAttribute("username");
        //session 不为空 放行
        if (username != null){
            return true;
        }else {
            //session 为空
            response.sendRedirect("/login.html");
            return false;
        }
    }
}
